<?php
/**
 * Created by PhpStorm.
 * User: songdewei
 * Date: 2017/10/15
 * Time: 下午5:25
 */
namespace Model\Api;

use Data\Company\CompanyContentModel;
use Data\Company\CompanyModel;

class CompanyController extends BaseController{
    /**
     *
     */
    public function index(){

    }

    /**
     * 获取企业基本信息
     */
    public function get(){
        $compnay_id = intval($_GET['company_id']);

        if (!$compnay_id) {
            $this->showAjaxError(1, 'Empty company_id value');
        }

        $withContent = $_GET['withContent'] ? 1 : 0;
        $company = (new CompanyModel())->where(array('company_id'=>$compnay_id))->getOne();
        if ($withContent) {
            $content = (new CompanyContentModel())->where(array('company_id'=>$compnay_id))->getOne();
            $company['content'] = $content['content'];
        }

        $this->showAjaxReturn($company);
    }

    /**
     *获取企业简介
     */
    public function get_content(){
        $compnay_id = intval($_GET['company_id']);

        if (!$compnay_id) {
            $this->showAjaxError(1, 'Empty company_id value');
        }

        $compnay_id = intval($_GET['company_id']);
        $content = (new CompanyContentModel())->where(array('company_id'=>$compnay_id))->getOne();
        $this->showAjaxReturn($content);
    }

    /**
     * 批量获取
     */
    public function batchget(){
        $catid = intval($_GET['catid']);
        $condition = $catid ? array('catid'=>$catid) : '';
        //偏移量
        $offset = $_GET['offset'] ? intval($_GET['offset']) : 0;
        //获取数目
        $count = $_GET['count'] ? intval($_GET['count']) : 10;



        $companylist = (new CompanyModel())->where($condition)->limit($offset, $count)->select();
        $this->showAjaxReturn($companylist);
    }

    /**
     * 更新企业信息
     */
    public function update(){
        $compnay_id = intval($_GET['company_id']);
        $company = $_GET['company'];
        (new CompanyModel())->where(array('company_id'=>$compnay_id))->data($company)->save();
        $this->showAjaxReturn();
    }

    /**
     * 删除企业信息
     */
    public function delete(){
        $compnay_id = intval($_GET['company_id']);
        //(new CompanyModel())->where(array('company_id'=>$compnay_id))->delete();
        (new CompanyModel())->deleteAllData($compnay_id);
        $this->showAjaxReturn();
    }

    /**
     * 添加企业信息
     */
    public function add(){
        $company = $_GET['company'];
        $company_id = (new CompanyModel())->data($company)->add();

        $content = $_GET['content'];
        (new CompanyContentModel())->data(array(
            'uid'=>$this->uid,
            'company_id'=>$company_id,
            'content'=>$content
        ))->add();

        $this->showAjaxReturn();
    }
}